﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HDT.GUI;
using DTO;
using BLL;

namespace HDT
{
    public partial class FrmMain : Form
    {
        private string ACCOUNT_IMAGE_FOLDER = "Image\\_Account\\";
        TKDangNhapDTO taiKhoan;

        public FrmMain()
        {
            InitializeComponent();
        }

        private void LoadFormToMainPanel(Form f)
        {
            pnlMain.Controls.Clear();
            f.TopLevel = false;
            pnlMain.Controls.Add(f);
            f.Show();
        }
#region
        private void MnDMucSP_Click(object sender, EventArgs e)
        {
            LoadFormToMainPanel(new FrmDMucMH(taiKhoan));
        }

        private void MnNhapHang_Click(object sender, EventArgs e)
        {
            LoadFormToMainPanel(new FrmLapPN(taiKhoan));
        }

        private void MnDSNhanVien_Click(object sender, EventArgs e)
        {
            LoadFormToMainPanel(new FrmDSNhanVien());
        }

        private void MnLapHD_Click(object sender, EventArgs e)
        {
            LoadFormToMainPanel(new FrmLapHD(taiKhoan));
        }

        private void MnLSuBH_Click(object sender, EventArgs e)
        {
            LoadFormToMainPanel(new FrmLSuBH());
        }

        private void MnBaoCaoDT_Click(object sender, EventArgs e)
        {
            LoadFormToMainPanel(new FrmBCDoanhThu());
        }

        private void MnBCTonKho_Click(object sender, EventArgs e)
        {
            LoadFormToMainPanel(new FrmBCTonKho());
        }

        private void MnXLKho_Click(object sender, EventArgs e)
        {
            LoadFormToMainPanel(new FrmXLKho());
        }

        private void MnBCLuong_Click(object sender, EventArgs e)
        {
            LoadFormToMainPanel(new FrmBCTHopLuong());
        }

        private void MnPhanQuyen_Click(object sender, EventArgs e)
        {
            LoadFormToMainPanel(new FrmPhanQuyen());
        }

        private void MnTrangChu_Click(object sender, EventArgs e)
        {
            LoadFormToMainPanel(new FrmChildMain());
        }

        private void label1_Click(object sender, EventArgs e)
        {
            MnTrangChu_Click(sender, e);
        }

        private void btnCauHinh_Click(object sender, EventArgs e)
        {
            FrmCauHinh frm_CauHinh = new FrmCauHinh();
            frm_CauHinh.ShowDialog();
        }

        private void llblDoiMK_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            FrmDoiMK frm_DoiMK = new FrmDoiMK(taiKhoan);
            frm_DoiMK.ShowDialog();
        }

        private void cấuHìnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmCauHinh frm_CauHinh = new FrmCauHinh();
            frm_CauHinh.ShowDialog();
        }

        private void MnCauHinh_Click(object sender, EventArgs e)
        {
            FrmCauHinh f = new FrmCauHinh();
            f.loadMain = new FrmCauHinh.myDelegate(LoadMain);
            LoadFormToMainPanel(f);
        }

        private void MnThamSo_Click(object sender, EventArgs e)
        {
            LoadFormToMainPanel(new FrmThamSo(taiKhoan));
        }
#endregion

        private void LoadMain()
        {
            if (taiKhoan != null)
            {
                LoadFormToMainPanel(new FrmChildMain());
            }
            else
            {
                LoadFormToMainPanel(new FrmDangNhap());
            }
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            PhanQuyen(null);
            FrmDangNhap f = new FrmDangNhap();
            f.setPhanQuyen = new FrmDangNhap.myDelegate(PhanQuyen);
            LoadFormToMainPanel(f);
        }

        private void PhanQuyen(TKDangNhapDTO taiKhoan)
        {
            this.taiKhoan = taiKhoan;
            try
            {
                NhanVienDTO nhanVien = new NhanVienBLL().GetByID(taiKhoan.MaNhanVien);
                pnlAccountControl.Visible = true;
                MnTrangChu.Visible = true;

                if (nhanVien == null)
                {
                    PhanQuyenAdmin(taiKhoan);
                }
                else
                {
                    try { 
                        new ChiTietMatHangBLL().XulyHSD(); 
                    }
                    catch { }
                    PhanQuyenNhanVien(nhanVien);
                }

                LoadFormToMainPanel(new FrmChildMain());
            }
            catch (Exception)
            {
                PhanQuyenDefault();
            }
        }

        private void llblDangXuat_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("Bạn thực sự muốn đăng xuất?", "CẢNH BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                this.FrmMain_Load(sender, e);
            }
        }

        private void LoadUserAvatar(string filename)
        {
            try {
                string path = ACCOUNT_IMAGE_FOLDER + filename;
                Image img = Image.FromFile(path);
                picAvatar.Image = img;
            }
            catch (Exception)
            {
                picAvatar.Image = picAvatar.ErrorImage;
            }
        }

 #region PhanQuyen
        private void PhanQuyenDefault()
        {
            MnTrangChu.Visible = false;
            MnAdmin.Visible = false;
            MnMuaSam.Visible = false;
            MnSanPham.Visible = false;
            MnNhanVien.Visible = false;
            MnHeThong.Visible = true;
            MnThamSo.Visible = false;
            MnCauHinh.Visible = true;
            MnAbout.Visible = true;
            MnHDSD.Visible = true;
            pnlUser.Visible = false;
        }

        private void PhanQuyenAdmin(TKDangNhapDTO taiKhoan)
        {
            MnTrangChu.Visible = true;
            MnAdmin.Visible = true;
            MnMuaSam.Visible = false;
            MnSanPham.Visible = false;
            MnNhanVien.Visible = false;
            MnHeThong.Visible = true;
            MnThamSo.Visible = false;
            MnCauHinh.Visible = true;
            MnAbout.Visible = true;
            MnHDSD.Visible = true;
            pnlUser.Visible = true;
            lblUsername.Text = taiKhoan.Username;
            lblPQ.Text = "Quản trị hệ thống";
            LoadUserAvatar("Administrator.bmp");
        }

        private void PhanQuyenNhanVien(NhanVienDTO nhanVien)
        {
            List<PhanQuyenDTO> PQs = new PhanQuyenBLL().Select("MaLoaiNV = '" + nhanVien.MaLoaiNhanVien + "'");
            PhanQuyenDTO PQ = PQs.Count == 0 ? new PhanQuyenDTO() : PQs[0];
            MnTrangChu.Visible = true;
            MnAdmin.Visible = false;
            
            MnMuaSam.Visible = true;
            MnLapHD.Visible = PQ.LapHoaDon;
            MnLSuBH.Visible = true;
            MnBaoCaoDT.Visible = true;


            MnSanPham.Visible = true;
            MnDMucSP.Visible = true;
            MnLapPN.Visible = true;
            MnXLKho.Visible = PQ.XuLyKho;
            toolStripSeparator4.Visible = PQ.XuLyKho;
            MnBCTonKho.Visible = true;

            MnNhanVien.Visible = true;
            mnLuongCN.Visible = true;
            MnDSNhanVien.Visible = true;
            MnBCLuong.Visible = PQ.QLNhanVien;

            MnHeThong.Visible = true;
            MnThamSo.Visible = true;
            MnCauHinh.Visible = true;
            MnAbout.Visible = true;
            MnHDSD.Visible = true;

            pnlUser.Visible = true;
            lblUsername.Text = nhanVien.MaNhanVien + " - " + nhanVien.HoTen;
            lblPQ.Text = new LoaiNhanVienBLL().GetByID(nhanVien.MaLoaiNhanVien).TenLoaiNV;
            LoadUserAvatar(nhanVien.HinhAnh); 
        }
 #endregion
    }
}
